<?php
class BuyController extends BuynodeController {
	
	public function iframe() {
		$rs = self::getBuyServie ()->listing ();
		$this->view ()->assign ( 'rs', $rs );
		$this->view ()->display ( "file:buy/iframe.tpl" );
	}
	
	public function admin() {
		$rs = self::getBuyServie ()->listing ();
		$this->view ()->assign ( 'rs', $rs );
		self::getLogService ()->add ( $this->_user_global ['real_name'], "访问订购后台系统" );
		$this->view ()->display ( "file:buy/admin.tpl" );
	}
	
	public function center() {
		$goods = self::getBuyServie ()->getRecommendGoods ();
		$this->view ()->assign ( 'goods', $goods );
		$this->view ()->display ( "file:buy/center.tpl" );
	}
	
	public function hot() {
		self::getLogService ()->add ( $this->_user_global ['real_name'], "查看排行榜" );
		$rs = self::getBuyServie ()->getGoodsBySales ();
		$this->view ()->assign ( 'goods', $rs );
		$this->view ()->display ( "file:buy/goods.tpl" );
	}
	
	public function add() {
		$cate = self::getBuyServie ()->getCategory ();
		$this->view ()->assign ( 'cate', $cate );
		$status = self::getBuyServie ()->getGoodsStatus ();
		$this->view ()->assign ( 'status', $status );
		$this->view ()->display ( "file:buy/add.tpl" );
	}
	
	public function addGoods() {
		$rs = self::getBuyServie ()->addGoods ( $_POST );
		$this->redirect ( $rs, "./index.php?buy/add" );
	}
	
	//编辑产品页面
	public function edit() {
		$rs = self::getBuyServie ()->getGoodsById ( $_GET ['id'] );
		$status = self::getBuyServie ()->getGoodsStatus ();
		$module = self::getBuyServie ()->getGoodsModule ();
		$cate = self::getBuyServie ()->getCategory ();
		$this->view ()->assign ( 'goods', $rs );
		$this->view ()->assign ( 'cate', $cate );
		
		$this->view ()->assign ( "module", $module );
		$this->view ()->assign ( 'status', $status );
		$this->view ()->display ( "file:buy/edit.tpl" );
	}
	
	public function upload() {
		$rs = self::getBuyServie ()->getGoodsBySKU ( $_GET ['sku'] );
		if (empty ( $rs ['base'] )) {
			echo "此产品尚未关联信息";
			exit ();
		}
		$this->view ()->assign ( 'content', $rs ['base'] );
		$this->view ()->display ( 'file:buy/img.tpl' );
	}
	
	//更新产品
	public function save() {
		$redirect = './index.php?buy/edit/?id=' . $_POST ['id'];
		$rs = self::getBuyServie ()->saveGoodsById ( $_POST );
		self::getLogService ()->add ( $this->_user_global ['real_name'], "更新产品$_POST[goods_name]" );
		$this->redirect ( $rs, $redirect );
	
	}
	
	public function search() {
		$rs = self::getBuyServie ()->getGoodsLikeTitle ( $_GET ['title'] );
		$title = urldecode ( $_GET ['title'] );
		self::getLogService ()->add ( $this->_user_global ['real_name'], "搜索$title" );
		
		$this->view ()->assign ( 'goods', $rs );
		$this->view ()->display ( "file:buy/goods.tpl" );
	}
	
	public function api() {
		$rs = self::getBuyServie ()->getGoodsAPI ( $_GET ['term'] );
		echo json_encode ( $rs );
	}
	
	public function flag() {
		$rs = self::getBuyServie ()->getGoodsByRemark ( $_GET ['remark'] );
		self::getLogService ()->add ( $this->_user_global ['real_name'], "查询特价" );
		
		$this->view ()->assign ( 'goods', $rs );
		$this->view ()->display ( "file:buy/goods.tpl" );
	}
	
	public function goods() {
		$rs = self::getBuyServie ()->getGoodsByCid ( $_GET ['cid'] );
		
		$this->view ()->assign ( 'cate', $rs ['cate'] );
		$cate = $rs ['cate'] ['category'];
		
		$this->view ()->assign ( 'goods', $rs ['goods'] );
		if ($_GET ['type'] == 1) {
			$template = "file:buy/goods.tpl";
		} else {
			$template = "file:buy/goodsimg.tpl";
		}
		
		$this->view ()->display ( $template);
	}
	
	//查看产品明细 iframe
	public function mx() {
		$goods = self::getBuyServie ()->getGoodsBySKU ( $_GET ['sku'] );
		$name = $goods ['base'] ['name'];
		self::getLogService ()->add ( $this->_user_global ['real_name'], "查看产品明细$name" );
		
		$this->view ()->assign ( "goods", $goods );
		$this->view ()->display ( "file:buy/mx.tpl" );
	}
	
	public function cart() {
		$rs = self::getBuyServie ()->getCart ();
		self::getLogService ()->add ( $this->_user_global ['real_name'], "查看购物车" );
		
		$this->view ()->assign ( "goods", $rs ['goods'] );
		$this->view ()->assign ( "totalcount", $rs ['total'] );
		
		$this->view ()->display ( "file:buy/cart.tpl" );
	}
	
	public static function getBuyServie() {
		return new BuyService ();
	}

}